package com.develop.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.MessageToByteEncoder;
import java.nio.charset.Charset;

import com.develop.BeanUtils.MsgHeadUtils;
import com.develop.vo.AlarmMsgVo;

public class AlarmEncoder extends MessageToByteEncoder<AlarmMsgVo> {
	public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
		super.write(ctx, msg, promise);
	}

	protected void encode(ChannelHandlerContext ctx, AlarmMsgVo msg, ByteBuf out) throws Exception {
		if (null == msg) {
			throw new Exception("msg is null");
		}

		byte[] bodyBytes = msg.getBody().getBytes(Charset.forName("utf-8"));

		out.writeBytes(MsgHeadUtils.shortToBytes2(AlarmMsgVo.getStartSign()));

		out.writeByte(msg.getMsgType());

		out.writeInt(msg.getTimeStamp());

		out.writeShort(msg.getLenOfBody());

		out.writeBytes(bodyBytes);
	}
}